﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTcms.BLL;
using DTcms.Common;
using DTcms.Web.Libs.WeiXin.business;
using DTcms.Web.Libs.WeiXin.lib;
using Newtonsoft.Json;

namespace DTcms.Web.WxSdjs
{
    /// <summary>
    /// PayHandler 的摘要说明
    /// </summary>
    public class PayHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            string openId = context.Request["openId"];
            if (string.IsNullOrWhiteSpace(openId))
                context.Response.Write(JsonConvert.SerializeObject(new Result
                {
                    IsOk = false,
                    Msg = "未能获取用户标识",
                }));
            switch (action)
            {
                case "buy":
                    //  订单支付
                    Buy(context, openId);
                    break;
                case "recharge":
                    //  充值
                    Recharge(context, openId);
                    break;
                case "queue":
                    //  排队
                    Queue(context, openId);
                    break;
                case "tip":
                    //  小费
                    Tip(context, openId);
                    break;
            }
            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        void Buy(HttpContext context, string openId)
        {
            Result<string> result = new Result<string>();
            try
            {
                string orderId = context.Request["orderId"] ?? string.Empty;
                string youHuiId = context.Request["youHuiId"] ?? string.Empty;

                pao_order orderBll = new pao_order();
                long orderIdLong = Convert.ToInt64(orderId.Substring(1));
                DTcms.Model.pao_order order = orderBll.GetModel(orderIdLong);


                string typeName = order.pao_w_typename;
                decimal? orderMoney = order.pao_money +
                                      order.pao_wipin_money -
                                      order.pao_youhui_money +
                                      order.pao_toubao_money;

                if (orderMoney == null)
                    orderMoney = 0;
                int totalFee = (int)(orderMoney.Value * 100);
                //totalFee = 1;

                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid = openId;
                jsApiPay.total_fee = totalFee;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(orderId, typeName);
                string wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数              

                result.IsOk = true;
                result.Data = wxJsApiParam;
            }
            catch (Exception exception)
            {
                result.IsOk = false;
                result.Msg = "发生错误";
            }

            context.Response.Write(JsonConvert.SerializeObject(result));
        }

        void Recharge(HttpContext context, string openId)
        {
            Result<string> result = new Result<string>();
            try
            {
                string orderId = context.Request["orderId"] ?? string.Empty;

                DTcms.BLL.pao_chongzhi bll = new DTcms.BLL.pao_chongzhi();
                long orderIdLong = Convert.ToInt64(orderId.Substring(1));
                DTcms.Model.pao_chongzhi model = bll.GetModel(orderIdLong);

                //this.Logger.WriteLog("1小东用户id:" + model.Fs_ur_UserId+"    状态:"+model.Fs_ur_Status);
                if (model.pao_status == 0)
                {
                    int totalFee = (int)(model.pao_chong_money.Value * 100);
                    //totalFee = 1;

                    JsApiPay jsApiPay = new JsApiPay();
                    jsApiPay.openid = openId;
                    jsApiPay.total_fee = totalFee;

                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(orderId, "微信充值");
                    string wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数              

                    result.IsOk = true;
                    result.Data = wxJsApiParam;
                }
            }
            catch (Exception exception)
            {
                result.IsOk = false;
                result.Msg = "发生错误";
            }

            context.Response.Write(JsonConvert.SerializeObject(result));
        }

        void Queue(HttpContext context, string openId)
        {
            Result<string> result = new Result<string>();
            try
            {
                string orderId = context.Request["orderId"] ?? string.Empty;
                string youHuiId = context.Request["youHuiId"] ?? string.Empty;

                pao_order orderBll = new pao_order();
                long orderIdLong = Convert.ToInt64(orderId.Substring(1));
                DTcms.Model.pao_order order = orderBll.GetModel(orderIdLong);


                string typeName = order.pao_w_typename;
                decimal? orderMoney = order.pao_money +
                                      order.pao_wipin_money -
                                      order.pao_youhui_money +
                                      order.pao_toubao_money;

                if (orderMoney == null)
                    orderMoney = 0;
                int totalFee = (int)(orderMoney.Value * 100);
                //totalFee = 1;

                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid = openId;
                jsApiPay.total_fee = totalFee;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(orderId, "排队");
                string wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数              

                result.IsOk = true;
                result.Data = wxJsApiParam;
            }
            catch (Exception exception)
            {
                result.IsOk = false;
                result.Msg = "发生错误";
            }

            context.Response.Write(JsonConvert.SerializeObject(result));
        }

        void Tip(HttpContext context, string openId)
        {
            Result<string> result = new Result<string>();
            try
            {
                string smallId = context.Request["smallId"] ?? string.Empty; //小费id
                string smallOrderId = context.Request["smallOrderId"] ?? string.Empty; //小费订单id

                //获取小费金额
                Model.pao_small_money smallMoney = new BLL.pao_small_money().GetModel(Convert.ToInt64(smallId));
                if (smallMoney == null)
                {
                    result.IsOk = false;
                    result.Msg = "未获取到小费记录";
                }
                else
                {
                    int totalFee = (int)(smallMoney.pao_money.Value * 100);
                    //totalFee = 1;

                    JsApiPay jsApiPay = new JsApiPay();
                    jsApiPay.openid = openId;
                    jsApiPay.total_fee = totalFee;

                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(smallOrderId, "排队");
                    string wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数              

                    result.IsOk = true;
                    result.Data = wxJsApiParam;
                }
            }
            catch (Exception exception)
            {
                result.IsOk = false;
                result.Msg = "发生错误";
            }

            context.Response.Write(JsonConvert.SerializeObject(result));
        }

    }
}